
//设置html根目录字体大小
require(['jquery','../modules/pageSize','../modules/popup'],function($,pageSetSize,popup){
    setRootFontSize(pageSetSize);
    var isAjax = false;
	var _isAjaxChange = false;
	var _yzTimer = 0;
    // 发送手机号 获取验证码
    function ajaxIphone(_mobile){
        $.ajax({
            type: "get",
            url: "/Home/Letter/verifyMobileCode"+getFixedExtraUrl(),
            data: {
                mobile: _mobile
            },
            dataType: "json",
            success: function(data) {
                if (data.stat){
                    timer(data.data);
                }else{
                    popup.alertPopup('验证码获取失败！');
                }
                isAjax = false;
            },
            error: function () {
                popup.alertPopup('服务器异常，请稍后重试！');
                isAjax = false;
            }
        });
    }
    // 倒计时方法
    function timer(_time) {
        var count = parseInt(_time)*60; // 默认时间
        var BtnCount = function() {
            if (count == 0) {
                clearTimeout(_yzTimer);
                $('.login_code').html('重获验证码');
                isAjax = false;
            } else {
                count--;
                $('.login_code').text(count < 10 ? ("0"+count) : count+"秒");
                _yzTimer = setTimeout(BtnCount, 1000);
            }
        };
        $('.login_code').text(count.toString()+"秒");
        count--;
        _yzTimer = setTimeout(BtnCount, 1000);
    }
    $(".login_code").on('click',function(){
        if(isAjax){
            return;
        }
        var _mobile = $("#mobile").val();
        var _myreg = _global.mbReg;
        if(_mobile){
            if(_myreg.test(_mobile)){
                isAjax = true;
                ajaxIphone(_mobile);
            } else {
                popup.alertPopup('请输入正确的手机号！');
                return;
            }
        }else{
            popup.alertPopup('请填写手机号码！');
            return;
        }
    });
	$("#change-mobile-btn").on("click", function(){
		if(_isAjaxChange) return;
		_isAjaxChange = true;
		var _mobile = $("#mobile").val().trim();
		var _code = $("#code").val().trim();
		if(_mobile == ""){
			popup.alertPopup('请输入手机号！');
			return;
		}
		if(_code == ""){
			popup.alertPopup('请输入验证码！');
			return;
		}
		popup.loadPopup();
		$.ajax({
            type: "post",
            url: ajax_mobile_url,
            data: {
                mobile: _mobile,
				code: _code,
				type: ajax_mobile_type
            },
            dataType: "json",
            success: function(data) {
				popup.removeLoad();
                if(data.status && ajax_mobile_type == 1){
					$("#mobile").val("");
					$("#code").val("");
					$("#change-mobile-btn").html("确定");
					$(".login_code").html("获取验证码");
					clearTimeout(_yzTimer);
					isAjax = false;
					ajax_mobile_type = 2;
                }
				popup.alertPopup(data.msg);
                if(data.url){
					setTimeout(function(){window.location.href = data.url;},1000);	
				}
                _isAjaxChange = false;
            },
            error: function () {
				popup.removeLoad();
                popup.alertPopup('服务器异常，请稍后重试！');
                _isAjaxChange = false;
            }
        });
	});
});
